{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "25a95a93-cddc-4867-938f-f0b802638d2e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "78b36e12-73e9-47f7-9b93-2bf37d1b0040",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Numpy 是 Python 科学计算的核心库。它提供了高性能的多维数组对象，并提供了处理这些数组的工具。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3d420527-7631-48de-b2ed-4a3c1187f10f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数组，numpy.ndarray 数组是由同一类型值组成的网格，并通过非负整数元组进行索引。\n",
    "# 数组的维度数是数组的秩；数组的形状是给出沿每个维度数组大小的整数元组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "60334692-0f0b-4b6c-915c-a91c02074d56",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([1, 2, 3])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "551aa659-6f9d-46ea-afb6-e4cb2f3f44a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1bc1c518-bae2-4b97-94f8-5ee74bb0f033",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3,)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "90665679-7034-42f1-a65e-188a6bd68217",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 3, 7],\n",
       "       [2, 5, 3]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.array([[1,3,7],[2,5,3]])\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a4d3ff75-2dbe-40d7-a5d8-91e28eff0001",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 3)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "1a1d89ae-4464-4086-89c0-d1148df42564",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "a575eab4-1bb2-4837-8d96-8012bdca86a2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "        0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "        0., 0., 0., 0.]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建数组的方式\n",
    "a = np.zeros((2,100))\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "fe811e6c-6b0b-43b3-a0ee-290d7f41901f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1.],\n",
       "       [1., 1., 1.]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.ones((2,3))\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "3712ef08-fcc9-4c9b-9131-81388564343d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[7, 7, 7],\n",
       "       [7, 7, 7]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = np.full((2,3), 7)\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "ccc40fcf-7d82-464a-8bdd-e6cc97356d57",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 1., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0., 0., 1., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],\n",
       "       [0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = np.eye(10) # 创建单位矩阵\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "ab501f81-46c1-4447-9288-2e7cf9aa4f27",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.44529443, 0.27441341],\n",
       "       [0.03743396, 0.37719753]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "e = np.random.random((2,2))\n",
    "e"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "570ca21e-d40e-48aa-94ae-6a9c77f95f2c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数组切片\n",
    "d[2:5,:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "3f2081f5-b720-42c6-957d-7eb16f9a85f2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0\n"
     ]
    }
   ],
   "source": [
    "print(d[5,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "f9f4fc38-831e-4897-aae8-8069e996582c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 4])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 其它访问方法\n",
    "a = np.array([[1,2], [3, 4], [5, 6]])\n",
    "a[[1,1],[0,1]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "a64e7524-69e9-441e-83c9-812d5a878bf0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3],\n",
       "       [ 4,  5,  6],\n",
       "       [ 7,  8,  9],\n",
       "       [10, 11, 12]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "86e29d2d-2a1e-4ece-bb90-831c304f4817",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2, 13],\n",
       "       [ 4,  5, 16],\n",
       "       [ 7,  8, 19],\n",
       "       [10, 11, 22]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[np.arange(4), 2] += 10 # 方便的选择多行/多列中的一列/一行\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "089308ef-78df-4773-84df-1887369a5e4c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 布尔索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "d394772a-4cd1-4139-819f-4178e3d721e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([[1,2], [3, 4], [5, 6]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "2d949a0b-9df5-4eff-b69f-c20437896861",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[False, False],\n",
       "       [ True,  True],\n",
       "       [ True,  True]])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bo_idx = (a > 2)\n",
    "bo_idx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "cdf236cc-3182-49f3-bb52-430c1ad99891",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 4],\n",
       "       [5, 6]])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[bo_idx].reshape((2,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "20997df9-a265-4e00-980d-e7f5782c16b2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[a >= 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "4d61c998-7492-4632-bf31-7a26d57ae05e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 指定数据类型\n",
    "x = np.array([1,2],dtype=np.float64)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "cdfd8aa7-bb05-49b9-a38c-29c98bbd7252",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 2.])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "390ed00d-3b3f-4ac3-91cf-68b8b587e4f2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "72c08a1f-382d-4bdb-88f6-c3620516da95",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数组间运算\n",
    "x = np.array([[1,2],[3,4]], dtype=np.float64)\n",
    "y = np.array([[5,6],[7,8]], dtype=np.float64)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "10b0795f-5b96-4d79-b80e-60cf08c66525",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 6.,  8.],\n",
       "       [10., 12.]])"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x + y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "a757210d-1230-484c-9ac2-88ac17ac9f16",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-4., -4.],\n",
       "       [-4., -4.]])"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x - y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "0f820211-be26-4fa5-b17d-aeed4bc8a679",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 5., 12.],\n",
       "       [21., 32.]])"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x * y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "cf1eaaa9-b8a9-46eb-b0af-adb96607064c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.2       , 0.33333333],\n",
       "       [0.42857143, 0.5       ]])"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x / y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "f5dc08d0-d884-48eb-aa81-ace30d645215",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0.],\n",
       "       [1., 0.]])"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y % x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "dc4c5099-e2a0-4577-824d-5261d0b869e6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.        , 1.41421356],\n",
       "       [1.73205081, 2.        ]])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sqrt(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "f1233375-2b53-43da-bc7a-79d8f96b435e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 矩阵运算\n",
    "x = np.array([[1,2],[3,4]])\n",
    "y = np.array([[5,6],[7,8]])\n",
    "\n",
    "v = np.array([9,10])\n",
    "w = np.array([11, 12])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "3da3e76a-fe64-43f4-853a-aab85ac9a1b2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int64(219)"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(v,w) # 矩阵乘法 1 * 2 x 2 * 1 = 1 * 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "4ad24fda-11c7-42fb-8f7a-8e593c773e6b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[19, 22],\n",
       "       [43, 50]])"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(x,y) # 矩阵乘法 2 * 2 x 2 * 2 = 2 * 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "b82aed8b-028f-4482-8f5d-18ccb71d17c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 求和函数\n",
    "x = np.array([[1,2],[3,4]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "b92ea640-60c6-4249-b887-506dd5339b5f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int64(10)"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "908e7c60-d4f8-47cd-96fd-99c90e97caed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 6])"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(x,axis = 0) # 沿0轴求和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "076103d0-89d1-4c93-a578-346654510ea5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 7])"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(x,axis = 1) # 沿1轴求和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "5d8d71ff-3626-406f-a549-a3767adaad32",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 3],\n",
       "       [2, 4]])"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = x.T # 矩阵转置\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "43311712-dfa0-4ff6-86ef-c3def4652428",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 广播机制\n",
    "# 通常我们有一个较小的数组和一个较大的数组，我们希望使用较小的数组多次对较大的数组进行某种操作。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
